$(function() {
    resertWindow();
    $(window).resize(function() {
        resertWindow();
    });

    doDefault(doShareFriend);
    $('.navigation').click(function() {
        openLocation();
    });


    $('.share-zhezhao').click(function(){
        $('.share-zhezhao').hide();
        $('.share-arrow').hide();
    });
})

function resertWindow() {
    $('.share-zhezhao').height(Number($(window).height())).width(Number($(window).width()));
}

//获取微信jssdk凭证
function doDefault(successCallBack,errorCallBack) {
    var successCallBack=successCallBack||false; 
    var errorCallBack=errorCallBack||false; 
    $.ajax({
        type: "post",
        url: "CommonApi/CommonWx/getWxJsSdkConfig",
        data: {
            url: window.location.href,
            park_id:$('#park_id').val()
        },
        async: true,
        dataType: 'json',
        success: function(data) {
            if (data.status) {
                var t = JSON.parse(data.v);
                wx.config({
                    beta: true,
                    debug: false,
                    appId: t.appId,
                    timestamp: t.timestamp,
                    nonceStr: t.nonceStr,
                    signature: t.signature,
                    jsApiList: ['checkJsApi', 'openLocation',"onMenuShareTimeline", "onMenuShareAppMessage"] // 必填，需要使用的JS接口列表
                });
                if(successCallBack){
                    successCallBack();
                }
            } else {
                errorAlert(data.msg);
                if(errorCallBack){
                    errorCallBack();
                }
            }
        }
    });
}

function openLocation() {
    if ($('#lat').val() == '' || $('#long').val() == '') {
        errorAlert('地址经纬度不完整');
        return;
    }
    wx.ready(function() {
        wx.checkJsApi({
            jsApiList: ['openLocation'], // 需要检测的JS接口列表，所有JS接口列表见附录2,  
            success: function(res) {
                if (res.checkResult.openLocation == false) {
                    errorAlert("您的微信版本太低！");
                    return;
                }
            }
        });
        wx.openLocation({
            type: 'gcj02',
            latitude: parseFloat($('#lat').val()), // 纬度，浮点数，范围为90 ~ -90
            longitude: parseFloat($('#long').val()), // 经度，浮点数，范围为180 ~ -180。
            name: $('.shop-name').html(), // 位置名
            address: $('.shop-address lable').html(), // 地址详情说明
            scale: 10, // 地图缩放级别,整形值,范围从1~28。默认为最大
        });
    });
};

function showShare(){
    $('.share-zhezhao').show();
    $('.share-arrow').show();
}

var doShareFriend=function(){
    wx.ready(function() {
        wx.checkJsApi({
            jsApiList: ['onMenuShareAppMessage','onMenuShareTimeline'], // 需要检测的JS接口列表，所有JS接口列表见附录2,  
            success: function(res) {
                if (res.checkResult.onMenuShareAppMessage == false) {
                    errorAlert("您的微信版本太低！");
                    return;
                }
            }
        });
        var img_url=getRootPath() +'/'+ $('#shop_img').val();
        // alert(img_url);
        // $('.share-zhezhao').show();
        // $('.share-arrow').show();
        wx.onMenuShareAppMessage({
            title: $('.shop-name').html(), // 分享标题
            desc: $('.shop-address lable').html(), // 分享描述
            link: window.location.href, // 分享链接
            imgUrl: img_url, // 分享图标
            type: 'link', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () {
                $('.share-zhezhao').hide();
                $('.share-arrow').hide();
            },
            cancel: function () {
                $('.share-zhezhao').hide();
                $('.share-arrow').hide();
            }
        });
        wx.onMenuShareTimeline({
            title: $('.shop-name').html(), // 分享标题
            desc: $('.shop-address lable').html(), // 分享描述
            link: window.location.href, // 分享链接
            imgUrl: img_url, // 分享图标
            success: function () {
                $('.share-zhezhao').hide();
                $('.share-arrow').hide();
            },
            cancel: function () {
                $('.share-zhezhao').hide();
                $('.share-arrow').hide();
            }
        });
    });
}